<?php


require('mc_table.php');

//koneksi ke database
$host = "localhost";
$user = "root";
$pass = "";
$dbnm = "db_rental_dev";
 
$conn = mysql_connect($host, $user, $pass);
if ($conn) {
	$open = mysql_select_db($dbnm);
	if (!$open) {
		die ("Database tidak dapat dibuka karena ".mysql_error());
	}
} else {
	die ("Server MySQL tidak terhubung karena ".mysql_error());
}
//akhir koneksi

function DateToIndo($date) { // fungsi atau method untuk mengubah tanggal ke format indonesia
   // variabel BulanIndo merupakan variabel array yang menyimpan nama-nama bulan
		$BulanIndo = array("Januari", "Februari", "Maret",
						   "April", "Mei", "Juni",
						   "Juli", "Agustus", "September",
						   "Oktober", "November", "Desember");
	
		$tahun = substr($date, 0, 4); // memisahkan format tahun menggunakan substring
		$bulan = substr($date, 5, 2); // memisahkan format bulan menggunakan substring
		$tgl   = substr($date, 8, 2); // memisahkan format tanggal menggunakan substring
		
		$result = $tgl . " " . $BulanIndo[(int)$bulan-1] . " ". $tahun;
		return($result);
}

function DateToIndoAngka($date) { // fungsi atau method untuk mengubah tanggal ke format indonesia
   // variabel BulanIndo merupakan variabel array yang menyimpan nama-nama bulan
	
		$tahun = substr($date, 0, 4); // memisahkan format tahun menggunakan substring
		$bulan = substr($date, 5, 2); // memisahkan format bulan menggunakan substring
		$tgl   = substr($date, 8, 2); // memisahkan format tanggal menggunakan substring
		
		$result = $tgl."/".$bulan."/".$tahun;
		return($result);
}


//get query dan variabel
$mulai = $_GET['mulai'];
$akhir = $_GET['akhir'];
$gudang = $_GET['gudang'];
$decode_cr = $_GET['cr_txt'];
$getquery = str_replace("+"," ",$_GET[query]);
$getquerykorporate = str_replace("+"," ",$_GET[query_korporate]);

//instansiasi fpdf
$pdf = new PDF_MC_Table('L','cm','A4');
$pdf->AliasNbPages();  
$pdf->AddPage();
$pdf->SetMargins(0.7,0.7,0.7);  

 #tampilkan judul laporan
$pdf->SetFont('Arial','B','16');
$pdf->Cell(0,0, "CIPAGANTI RENTAL CAR & TRAVEL", 0, 1, 'C');
$pdf->Cell(28.5,2, "REKAPITULASI UMUR PIUTANG DAGANG", 0, 1, 'C');
$pdf->SetFont('Arial','B','12');
$pdf->Cell(8,3, "CABANG : ".$gudang,0,0,'L');
$pdf->Cell(20,3, "PERIODE : ".DateToIndo($mulai)." - ".DateToIndo($akhir),0,0,'R');

//membuat kop tabel 
$y=$pdf->GetY();  
$pdf->SetY($y+2.5);  
$pdf->SetFont('Arial','B',11);
$pdf->SetWidths(array(9, 3.2, 3.2, 3.2, 3.2, 3.2, 3.3));
$pdf->SetHeight(0.1);
$pdf->SetAligns(Array('C','C','C','C','C','C','C'));
$pdf->Row(array("Customer", "0 -30 hari", "31 - 60 hari", "61 - 90", "91 - 120 hari", "Lebih dari 120 hari", "Total"));

$pdf->SetFont('Arial','B',9);
$pdf->SetWidths(array(28.3));
$pdf->Row(array("PIUTANG SEWA KENDARAAN RETAIL"));

//ISI TABEL
$pdf->SetFont('Arial','',9);
$pdf->SetWidths(array(9, 3.2, 3.2, 3.2, 3.2, 3.2, 3.3));
$pdf->SetHeight(0.1);
$pdf->SetAligns(Array('C','C','C','C','C','C','C'));

$query_data = $getquery;
$query = mysql_query ($query_data) or die("Query gagal");

while($result= mysql_fetch_array( $query )){
	
	$pdf->Row(array($result['nama']."\n".$result['alamat'] , number_format($result['satu'],0,',','.').",-", number_format($result['dua'],0,',','.').",-", number_format($result['tiga'],0,',','.').",-", number_format($result['empat'],0,',','.').",-", number_format($result['lima'],0,',','.').",-", number_format($result['satu'] + $result['dua'] + $result['tiga'] + $result['empat'] + $result['lima'],0,',','.').",-" ));
	
	$totalsatu += $result['satu'];
    $totaldua += $result['dua'];
    $totaltiga += $result['tiga'];
	$totalempat += $result['empat'];
    $totallima += $result['totalbayar'];
    $totalretail += $result['satu'] + $result['dua'] + $result['tiga'] + $result['empat']+$result['lima'];
	
}

$pdf->SetFont('Arial','B',9);
$pdf->SetWidths(array(9, 3.2, 3.2, 3.2, 3.2, 3.2, 3.3));
$pdf->Row(array("SUBTOTAL",number_format($totalsatu,0,',','.').",-",number_format($totaldua,0,',','.').",-", number_format($totaltiga,0,',','.').",-",number_format($totalempat,0,',','.').",-", number_format($totallima,0,',','.').",-", number_format($totalretail,0,',','.').",-"));
$pdf->SetWidths(array(28.3));
$pdf->Row(array("PIUTANG SEWA KENDARAAN NON RETAIL (CORPORATE)"));

$pdf->SetFont('Arial','',9);
$pdf->SetWidths(array(9, 3.2, 3.2, 3.2, 3.2, 3.2, 3.3));
$pdf->SetHeight(0.1);
$pdf->SetAligns(Array('C','C','C','C','C','C','C'));

$query_datakorporate = $getquerykorporate;
$querykorp = mysql_query ($query_datakorporate) or die("Query gagal");

while($resultkorp= mysql_fetch_array( $querykorp )){
	
	$pdf->Row(array($resultkorp['nama']."\n".$resultkorp['alamat'] , number_format($resultkorp['satu'],0,',','.').",-", number_format($resultkorp['dua'],0,',','.').",-", number_format($resultkorp['tiga'],0,',','.').",-", number_format($resultkorp['empat'],0,',','.').",-", number_format($resultkorp['lima'],0,',','.').",-", number_format($resultkorp['satu'] + $resultkorp['dua'] + $resultkorp['tiga'] + $resultkorp['empat'] + $resultkorp['lima'],0,',','.').",-" ));
	
	$totalsatukorporate += $resultkorp['satu'];
    $totalduakorporate += $resultkorp['dua'];
    $totaltigakorporate += $resultkorp['tiga'];
	$totalempatkorporate += $resultkorp['empat'];
    $totallimakorporate += $resultkorp['totalbayar'];
    $totalkorporate += $resultkorp['satu'] + $resultkorp['dua'] + $resultkorp['tiga'] + $resultkorp['empat']+$resultkorp['lima'];
        
	
}

$pdf->SetFont('Arial','B',9);
$pdf->SetWidths(array(9, 3.2, 3.2, 3.2, 3.2, 3.2, 3.3));
$pdf->Row(array("SUBTOTAL",number_format($totalsatukorporate,0,',','.').",-",number_format($totalduakorpotate,0,',','.').",-", number_format($totaltigakorporate,0,',','.').",-",number_format($totalempatkorporate,0,',','.').",-", number_format($totallimakorporate,0,',','.').",-", number_format($totalkorporate,0,',','.').",-"));
$pdf->SetWidths(array(28.3));
$pdf->Row(array(""));
$pdf->SetWidths(array(25,3.3));
$pdf->SetFont('Arial','B',10);
$pdf->Row(array("GRAND TOTAL",number_format($totalretail+$totalkorporate,0,',','.').",-" ));


$pdf->Ln();
$pdf->Output('Laporan Rekapitulasi Umur Piutang Dagang Cabang '.$gudang.' Periode '.DateToIndo($mulai)." - ".DateToIndo($akhir).'.pdf','D');


?>